package com.xiaoke.web.controller;

import com.xiaoke.entity.Admin;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SupportAction {
    /**
     * 获得String类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public String getString(HttpServletRequest request, String name) {
        if (StringUtils.isNotEmpty(name)) {
            return request.getParameter(name);
        }
        return null;
    }

    /**
     * 获得Integer类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public int getInteger(HttpServletRequest request, String name) {
        String value = getString(request, name);
        if (StringUtils.isNotEmpty(value)) {
            try {
                return Integer.parseInt(value);
            } catch (Exception e) {
                return 0;
            }

        }
        return 0;
    }

    /**
     * 获得Double类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public double getDouble(HttpServletRequest request, String name) {
        String value = getString(request, name);
        if (StringUtils.isNotEmpty(value)) {
            try {
                return Double.parseDouble(value);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

        }
        return 0;
    }

    /**
     * 获得Boolean类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public boolean getBoolean(HttpServletRequest request, String name) {
        String value = getString(request, name);
        if (StringUtils.isNotEmpty(value)) {
            try {
                return Boolean.getBoolean(value);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

        }
        return false;
    }

    /**
     * 向客户端写数据
     *
     * @param response
     * @param info
     */
    public void writeInfo(HttpServletResponse response, String info) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.getWriter().write(info);
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void showBox(HttpServletResponse response, String context) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print("<script>alert('" + context + "');history.go(-1);</script>");
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Admin getCurrentUserInSession(HttpSession session) {
        return (Admin) session.getAttribute("user");
    }

    public static void putCurrentUserToSession(HttpSession session, Admin user) {
        session.setAttribute("user", user);
    }

    public String formatDate(Date date) {
        String dateStr = null;
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateStr = sdFormat.format(date);
        return dateStr;
    }

    public Date parseString(String time) throws ParseException {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        return sdFormat.parse(time);
    }

}
